package cn.kailangtu.dataspot.activify;

import android.graphics.Color;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.navigation.fragment.NavHostFragment;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.ValueFormatter;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.litepal.LitePal;

import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import cn.kailangtu.commonevents.eventEntity.AutoDataRefreshEvent;
import cn.kailangtu.commonevents.eventEntity.BluetoothConnectChangedEvent;
import cn.kailangtu.commonevents.eventEntity.DataBeanChangedEvent;
import cn.kailangtu.commonevents.eventEntity.EthernetConnectChangedEvent;
import cn.kailangtu.commonevents.eventEntity.FragmentChangedEvent;
import cn.kailangtu.commonevents.eventEntity.MobileConnectChangedEvent;
import cn.kailangtu.commonevents.eventEntity.OnFragmentChangeEvent;
import cn.kailangtu.commonevents.eventEntity.OpenCameraViewEvent;
import cn.kailangtu.commonevents.eventEntity.SystemErrCodeEvent;
import cn.kailangtu.commonevents.eventEntity.TemperatureDataEvent;
import cn.kailangtu.commonevents.eventEntity.TitleClickEvent;
import cn.kailangtu.commonevents.eventEntity.TotalMainDataChangedEvent;
import cn.kailangtu.commonevents.eventEntity.UsbConnectChangedEvent;
import cn.kailangtu.commonevents.eventEntity.WifiConnectChangedEvent;
import cn.kailangtu.commontools.APPSPUtils;
import cn.kailangtu.commontools.AppUtils;
import cn.kailangtu.commontools.AppVariable;
import cn.kailangtu.commontools.DateTimeUtils;
import cn.kailangtu.commontools.LineChatUtils;
import cn.kailangtu.dataspot.R;
import cn.kailangtu.dataspot.camera.AvcPlayerHelper;
import cn.kailangtu.dataspot.data.BaseData;
import cn.kailangtu.dataspot.data.ControlTree;
import cn.kailangtu.dataspot.data.HighData;
import cn.kailangtu.dataspot.data.NewDataContainer;
import cn.kailangtu.dataspot.data.PageIndex;
import cn.kailangtu.dataspot.data.context.SelectContext;
import cn.kailangtu.dataspot.data.consts.ControlAction;
import cn.kailangtu.dataspot.data.status.ConnectStatus;
import cn.kailangtu.dataspot.databinding.FragmentRealPictureLowBinding;
import cn.kailangtu.dataspot.db.ExceptionLogEntity;
import cn.kailangtu.dataspot.status;
import cn.kailangtu.dataspot.tasks.BaseTask;
import cn.kailangtu.dataspot.total.RealViewTotalData;
import cn.kailangtu.dataspot.total.TotalData;

public class RealPictureLowFragment extends BaseFragment {
    DecimalFormat df = new DecimalFormat("#.##");
    private FragmentRealPictureLowBinding binding;

    private BaseData data = new BaseData();


    boolean isRun = true;

    @Override
    public View onCreateView(
            @NonNull LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState
    ) {

        binding = FragmentRealPictureLowBinding.inflate(inflater, container, false);
        return binding.getRoot();

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(AutoDataRefreshEvent event){
        resetData();
    }
    public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        realViewTotalData = new RealViewTotalData(binding.lineChartDianYa,binding.lineChartDianliu);
        resetElement();
        resetData();
        initListen();
        EventBus.getDefault().post(new FragmentChangedEvent(R.layout.fragment_real_picture_low));



        binding.lineChartDianYa.setNoDataText("");
        binding.lineChartDianliu.setNoDataText("");

        binding.tvSN.setText(AppUtils.getInstance().getAndroidSN());

        String strLocalIp = AppUtils.getInstance().getIpAddress();
        String strVpnIp = AppUtils.getInstance().getVpnIpAddress();
        if(!strLocalIp.isEmpty() && !strLocalIp.equals("0.0.0.0"))
        {
            binding.tvLocalIP.setText("内网:"+strLocalIp);
            binding.tvLocalIP.setVisibility(View.VISIBLE);
        }
        else{
            binding.tvLocalIP.setVisibility(View.GONE);
        }
        if(!strVpnIp.isEmpty())
        {
            binding.tvCloundIP.setText("外网:"+strVpnIp);
            binding.tvCloundIP.setVisibility(View.VISIBLE);
        }
        else
        {
            binding.tvCloundIP.setVisibility(View.GONE);
        }

        new Thread(new Runnable() {
            @Override
            public void run() {
                int index = 0;
                while(index < 10) {
                    TotalData.totalSecond(10, 1, 0);
                    index++;
                    SystemClock.sleep(1000);
                    Log.d("IOIOIUOIUIUIUIU","做了");

                }
            }
        }).start();

        initIPCameraView1();
    }

    private void resetData()
    {

        data.getVoltageA().setValue(HighData.getInstance().getValue(3506));
        data.getVoltageB().setValue(HighData.getInstance().getValue(3507));
        data.getVoltageC().setValue(HighData.getInstance().getValue(3508));

        data.getElectricA().setValue(HighData.getInstance().getValue(3509));
        data.getElectricB().setValue(HighData.getInstance().getValue(3510));
        data.getElectricC().setValue(HighData.getInstance().getValue(3511));

        data.getCommontTempurature().setValue(HighData.getInstance().getValue(3512));

        data.getElectricCount().setValue(HighData.getInstance().getValue(3515));
        data.getSwitchRunTime().setValue(HighData.getInstance().getValue(3513));
        data.getOpeningCount().setValue(HighData.getInstance().getValue(3514));
        data.getRatedVoltage().setValue(formatRated(HighData.getInstance().getValue( 4500)));
        data.getOverloadCurrent().setValue(HighData.getInstance().getValue(4501));
        data.getRatedCurrent().setValue(Math.toIntExact(Math.round(data.getOverloadCurrent().getValue().intValue() * 1.2)));
        data.getShortCircuitCurrent().setValue(HighData.getInstance().getValue(4504)* data.getOverloadCurrent().getValue() / 10);

        data.getRunStatus().setValue(HighData.getInstance().getValue(3503));

        data.getMatherLineTempurature().setValue(HighData.getInstance().getValue(3584));
        data.getMatherLineTempurature().setValue2(HighData.getInstance().getValue(3585));
        data.getMatherLineTempurature().setValue3(HighData.getInstance().getValue(3586));

        data.getOutLineTempurature().setValue(HighData.getInstance().getValue(3587));
        data.getOutLineTempurature().setValue2(HighData.getInstance().getValue(3588));
        data.getOutLineTempurature().setValue3(HighData.getInstance().getValue(3589));

        int add_3504 = HighData.getInstance().getValue(3504);
        if(add_3504 == 1)
        {
            binding.iv3504Open.setVisibility(View.GONE);
            binding.iv3504Close.setVisibility(View.VISIBLE);
            binding.btn3504.setText("合 闸");

        }
        else if(add_3504 == 0) {
            binding.iv3504Open.setVisibility(View.VISIBLE);
            binding.iv3504Close.setVisibility(View.GONE);
            binding.btn3504.setText("分 闸");
        }






//        binding.tvElectricCountLabel.setText(data.getElectricCount().getLabel());
//        binding.tvElectricCountValue.setText(data.getElectricCount().getValue()+"KWH");




        binding.tvRunStatusLabel.setText(data.getRunStatus().getLabel());
        binding.tvRunStatusValue.setText(BaseData.getRunStatusLow( data.getRunStatus().getValue()));



        binding.tvRealPictureEA1.setText("Ia:"+
                data.getElectricA().getValue() +"A"
        );

        binding.tvRealPictureEA2.setText("Ib:"+
                data.getElectricB().getValue() +"A"
        );

        binding.tvRealPictureEA3.setText("Ic:"+
                data.getElectricB().getValue()
                +"A"
        );
        //binding.tvRealPictureEB.setText("电流值B:"+data.getElectricB().getValue());
        //binding.tvRealPictureEC.setText("电流值C:"+data.getElectricC().getValue());

        binding.tvRealPictureVA.setText("系统电压:"
                +(data.getVoltageA().getValue() + data.getVoltageB().getValue()+ data.getVoltageC().getValue()) / 3
                +"V"
        );
        //binding.tvRealPictureVB.setText("电压值B:"+data.getVoltageB().getValue());
        //binding.tvRealPictureVC.setText("电压值C:"+data.getVoltageC().getValue());
      //  int avgTemp = (status.temp_3584+status.temp_3585+status.temp_3586+status.temp_3587+status.temp_3588+status.temp_3589) /6;
        binding.tvRealPictureTemper.setText("电阻:"+ new Float(data.getCommontTempurature().getValue()) / 10 +"kΩ");
        binding.tvRealPictureTemperEx.setText("温度:"+data.getSwitchRunTime().getValue()+"°C");
       // binding.tvRealPictureTemperExEx.setText("接线柱平均温度:"+avgTemp);

        binding.DATA12.setText(NewDataContainer.getInstance().get有功率显示()+"kW");
        binding.DATA13.setText(NewDataContainer.getInstance().get无功率显示()+"kvar");
        binding.DATA30.setText(NewDataContainer.getInstance().get视在功率显示()+"kVA");
        binding.DATA16.setText(NewDataContainer.getInstance().get正有功电能()+"kWh");
        binding.DATA18.setText(NewDataContainer.getInstance().get正无功电能()+"kvarh");
        binding.DATA20.setText(NewDataContainer.getInstance().get负有功电能()+"kWh");
        binding.DATA22.setText(NewDataContainer.getInstance().get负无功电能()+"kvarh");
        binding.DATA14.setText(NewDataContainer.getInstance().get功率因素());

        String msg =
                "系统电压:"+HighData.getInstance().getValue(4500)
                + "\r\n互感器:"+HighData.getInstance().getValue(4502)
                + "\r\n互感器1:"+NewDataContainer.getInstance().get互感器1()
                + "\r\n电压选择1："+NewDataContainer.getInstance().get电压选择1()
                + "\r\n有功电能1："+NewDataContainer.getInstance().get有功电能1()
                + "\r\n有功电能2："+NewDataContainer.getInstance().get有功电能2()
                + "\r\n无功电能1："+NewDataContainer.getInstance().get无功电能1()
                + "\r\n无功电能2："+NewDataContainer.getInstance().get无功电能2()
                + "\r\n有功率："+NewDataContainer.getInstance().get有功率()
                + "\r\n无功率："+NewDataContainer.getInstance().get无功率()
                + "\r\n视在功率："+NewDataContainer.getInstance().get视在功率()
                        + "\r\n电压A："+NewDataContainer.getInstance().get电压A()
                        + "\r\n电压B："+NewDataContainer.getInstance().get电压B()
                        + "\r\n电压C："+NewDataContainer.getInstance().get电压C()
                        + "\r\n电流A："+NewDataContainer.getInstance().get电流A()
                        + "\r\n电流B："+NewDataContainer.getInstance().get电流B()
                        + "\r\n电流C："+NewDataContainer.getInstance().get电流C()
                        + "\r\nA相功率因素："+NewDataContainer.getInstance().getA相功率因素()
                        + "\r\nB相功率因素："+NewDataContainer.getInstance().getB相功率因素()
                        + "\r\nC相功率因素："+NewDataContainer.getInstance().getC相功率因素()
                        +"\r\n3506:"+HighData.getInstance().getValue(3506)
                        +"\r\n3507:"+HighData.getInstance().getValue(3507)
                        +"\r\n3508:"+HighData.getInstance().getValue(3508)
                        +"\r\n平均："+(HighData.getInstance().getValue(3506)+HighData.getInstance().getValue(3507)+HighData.getInstance().getValue(3508)) /3 ;
        binding.tvLowData.setText(msg);
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        isRun = false;
        binding = null;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(WifiConnectChangedEvent event)
    {
        if(ConnectStatus.isWifiConnected())
        {
            binding.ivRealPictureWIFILow.setImageResource(R.drawable.element_realpicture_top_wifi_used);
        }
        else{
            binding.ivRealPictureWIFILow.setImageResource(R.drawable.element_realpicture_top_wifi);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(TitleClickEvent event)
    {
        if(binding.rlTestView.getVisibility()== View.GONE)
        {
            binding.rlTestView.setVisibility(View.VISIBLE);
        }
        else
        {
            binding.rlTestView.setVisibility(View.GONE);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(UsbConnectChangedEvent event)
    {
        if(ConnectStatus.isUsbConnected())
        {
            binding.ivRealPictureUSBLow.setImageResource(R.drawable.element_realpicture_top_usb_used);
        }
        else{
            binding.ivRealPictureUSBLow.setImageResource(R.drawable.element_realpicture_top_usb);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(MobileConnectChangedEvent event)
    {
        if(ConnectStatus.isMobileConnected())
        {
            binding.ivRealPictureMOBILELow.setImageResource(R.drawable.element_realpicture_top_tower_used);
        }
        else{
            binding.ivRealPictureMOBILELow.setImageResource(R.drawable.element_realpicture_top_tower);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(EthernetConnectChangedEvent event)
    {
        if(ConnectStatus.isEthernetConnected())
        {
            binding.ivRealPictureETHERNETLow.setImageResource(R.drawable.element_realpicture_top_cross_used);
        }
        else{
            binding.ivRealPictureETHERNETLow.setImageResource(R.drawable.element_realpicture_top_cross);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(BluetoothConnectChangedEvent event)
    {
        if(ConnectStatus.isBluetoothConnected())
        {
            binding.ivRealPictureBLUETOOTHLow.setImageResource(R.drawable.element_realpicture_top_bluetooth_used);
        }
        else{
            binding.ivRealPictureBLUETOOTHLow.setImageResource(R.drawable.element_realpicture_top_bluetooth);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(OnFragmentChangeEvent event) {
        // Do something
        if(R.layout.fragment_factory_setting_low == event.getTargetFragmentId())
        {
            NavHostFragment.findNavController(RealPictureLowFragment.this)
                    .navigate(R.id.action_RealPictureLowFragment_to_FactorySettingLowFragment);
        }
        else if(R.layout.fragment_switch_test_low == event.getTargetFragmentId())
        {
            NavHostFragment.findNavController(RealPictureLowFragment.this)
                    .navigate(R.id.action_RealPictureLowFragment_to_SwitchTestLowFragment);
        }
        else if(R.layout.fragment_common_setting_low == event.getTargetFragmentId())
        {
            NavHostFragment.findNavController(RealPictureLowFragment.this)
                    .navigate(R.id.action_RealPictureLowFragment_to_CommonSettingLowFragment);
        }
        else if(R.layout.fragment_exception_log_low == event.getTargetFragmentId())
        {
            NavHostFragment.findNavController(RealPictureLowFragment.this)
                    .navigate(R.id.action_RealPictureLowFragment_to_ExceptionLogLowFragment);
        }
        else if(R.layout.fragment_real_picture_low == event.getTargetFragmentId())
        {

        }
    }

    private void initListen(){
        binding.tvLocalIP.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(new TitleClickEvent());
            }
        });
    }

    String lastMsg1 = "暂无数据";
    String lastMsg2 = "暂无数据";

    private void showCamera(){
        EventBus.getDefault().post(new OpenCameraViewEvent());
    }


    @Override
    protected int processControlsEvent(int eventType) {
        if(eventType == ControlAction.Selection)
        {
            TextView textView =  this.getActivity().findViewById(controlTree.getSelectNode().getIndex());
            SelectContext.setSelectedView(textView);
        }
        else if(eventType == ControlAction.Entry){
            TextView textView =  this.getActivity().findViewById(controlTree.getSelectNode().getIndex());
            textView.callOnClick();
        }
        return 0;
    }

    @Override
    protected void buildControlTree() {
        controlTree = ControlTree.getLowDataControlTree();
    }

    @Override
    protected void resetPageIndex() {

        if (status.localToPlc != 2) {
            status.localPageIndex = PageIndex.L_MAIN;
            BaseTask.toNewPage(status.localPageIndex);
        }
        else
        {
            if(status.plcPageIndex == PageIndex.L_MAIN){
                status.localPageIndex = PageIndex.L_MAIN;
            }
        }
    }

    @Override
    void resetPlatform() {
        if(AppVariable.platform_version == 450 && AppVariable.need_reset_view)
        {
            int top = -20;
            int left = 0;

            ViewGroup.MarginLayoutParams layoutParams =  (ViewGroup.MarginLayoutParams)binding.llRealPictureContainer.getLayoutParams();
            int newtop =  layoutParams.topMargin + top;
            int newleft = layoutParams.leftMargin + left;


            layoutParams.setMargins(newleft,newtop,0,0);
            binding.llRealPictureContainer.setLayoutParams(layoutParams);


        }
    }


    RealViewTotalData realViewTotalData = null;

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(TotalMainDataChangedEvent event){


        if(realViewTotalData != null)
        {
            realViewTotalData.resetData(event);
        }

    }

    private int formatRated(int source)
    {
        if(source == 0)
        {
            return 6000;
        }else if(source == 1)
        {
            return 10000;
        }
        return source;
    }

    private void resetElement()
    {
        if(APPSPUtils.getInstance().get红外测温视频Switch().equals("开"))
        {

            binding.vLine3.setVisibility(View.VISIBLE);
            //lineChat变zai
            ViewGroup.LayoutParams layoutParamsDianliu =  binding.lineChartDianliu.getLayoutParams();
            ViewGroup.LayoutParams layoutParamsDianYa = binding.lineChartDianYa.getLayoutParams();
            ViewGroup.LayoutParams layoutParamsMiddle = binding.llMiddle.getLayoutParams();
            ViewGroup.LayoutParams layoutParamsChartContainer =  binding.rlChartContainer.getLayoutParams();

            ViewGroup.MarginLayoutParams layoutParamsData22Label = (ViewGroup.MarginLayoutParams) binding.DATA22Label.getLayoutParams();
            ViewGroup.MarginLayoutParams layoutParamsData22 = (ViewGroup.MarginLayoutParams) binding.DATA22.getLayoutParams();

            ViewGroup.MarginLayoutParams layoutParamsData20Label = (ViewGroup.MarginLayoutParams) binding.DATA20Label.getLayoutParams();
            ViewGroup.MarginLayoutParams layoutParamsData20 = (ViewGroup.MarginLayoutParams) binding.DATA20.getLayoutParams();


            ViewGroup.LayoutParams layoutParams12L = binding.tvDATA12Label.getLayoutParams();
            ViewGroup.LayoutParams layoutParams13L = binding.tvDATA13Label.getLayoutParams();
            ViewGroup.LayoutParams layoutParams30L = binding.tvDATA30Label.getLayoutParams();
            ViewGroup.LayoutParams layoutParams12V = binding.DATA12.getLayoutParams();
            ViewGroup.LayoutParams layoutParams13V = binding.DATA13.getLayoutParams();
            ViewGroup.LayoutParams layoutParams30V = binding.DATA30.getLayoutParams();

            layoutParams12L.width = 80;
            layoutParams13L.width = 80;
            layoutParams30L.width = 80;
            layoutParams12V.width = 90;
            layoutParams13V.width = 90;
            layoutParams30V.width = 90;

            binding.tvDATA12Label.setLayoutParams(layoutParams12L);
            binding.tvDATA13Label.setLayoutParams(layoutParams13L);
            binding.tvDATA30Label.setLayoutParams(layoutParams30L);
            binding.DATA12.setLayoutParams(layoutParams12V);
            binding.DATA13.setLayoutParams(layoutParams13V);
            binding.DATA30.setLayoutParams(layoutParams30V);




            layoutParamsData22Label.leftMargin = 0;
            layoutParamsData22Label.topMargin = 90;
            binding.DATA22Label.setLayoutParams(layoutParamsData22Label);

            layoutParamsData22.leftMargin = 120;
            layoutParamsData22.topMargin = 90;
            binding.DATA22.setLayoutParams(layoutParamsData22);

            layoutParamsData20Label.leftMargin = 0;
            layoutParamsData20Label.topMargin = 60;
            binding.DATA20Label.setLayoutParams(layoutParamsData20Label);

            layoutParamsData20.leftMargin = 120;
            layoutParamsData20.topMargin = 60;
            binding.DATA20.setLayoutParams(layoutParamsData20);

            binding.llRight.setVisibility(View.VISIBLE);

            layoutParamsDianliu.width = 500;
            layoutParamsDianYa.width = 500;
            layoutParamsMiddle.width = 540;

            layoutParamsChartContainer.height = 240;
            binding.rlChartContainer.setLayoutParams(layoutParamsChartContainer);





            ViewGroup.LayoutParams layoutParamsVA = binding.tvRealPictureVA.getLayoutParams();
            ViewGroup.LayoutParams layoutParamsEA1 =  binding.tvRealPictureEA1.getLayoutParams();
            ViewGroup.LayoutParams layoutParamsEA2 = binding.tvRealPictureEA2.getLayoutParams();
            ViewGroup.LayoutParams layoutParamsEA3 = binding.tvRealPictureEA3.getLayoutParams();
            ViewGroup.LayoutParams layoutParamsTemper =   binding.tvRealPictureTemper.getLayoutParams();
       //     ViewGroup.LayoutParams layoutParamsLine1 = binding.vLine1.getLayoutParams();
        //    ViewGroup.LayoutParams layoutParamsLine2 =  binding.vLine2.getLayoutParams();

            binding.tvRealPictureVA.setTextSize(18f);
            binding.tvRealPictureEA1.setTextSize(18f);
            binding.tvRealPictureEA2.setTextSize(18f);
            binding.tvRealPictureEA3.setTextSize(18f);
            binding.tvRealPictureTemper.setTextSize(18f);
            binding.tvRealPictureTemperEx.setTextSize(18f);
            binding.tvRealPictureTemperExEx.setTextSize(18f);


            layoutParamsVA.width = 150;
            layoutParamsEA1.width = 80;
            layoutParamsEA2.width = 80;
            layoutParamsEA3.width = 80;
            layoutParamsTemper.width = 120;
        //    layoutParamsLine1.width = 500;
         //   layoutParamsLine2.width = 500;
            binding.tvRealPictureVA.setLayoutParams(layoutParamsVA);
            binding.tvRealPictureEA1.setLayoutParams(layoutParamsEA1);
            binding.tvRealPictureEA2.setLayoutParams(layoutParamsEA2);
            binding.tvRealPictureEA3.setLayoutParams(layoutParamsEA3);
            binding.tvRealPictureTemper.setLayoutParams(layoutParamsTemper);
         //   binding.vLine1.setLayoutParams(layoutParamsLine1);
          //  binding.vLine2.setLayoutParams(layoutParamsLine2);



            binding.lineChartDianliu.setLayoutParams(layoutParamsDianliu);
            binding.lineChartDianYa.setLayoutParams(layoutParamsDianYa);

            binding.llMiddle.setLayoutParams(layoutParamsMiddle);

            realViewTotalData.resetParams();
        }
    }

    AvcPlayerHelper avcPlayerHelper = new AvcPlayerHelper();
    private void initIPCameraView1(){
        avcPlayerHelper.initIPCameraView(binding.apMain);
    }

    //DecimalFormat df = new DecimalFormat("#.#");
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(TemperatureDataEvent event){

        if (event.getIndex() == 0) {
            binding.apSub.setImageBitmap(event.getImage());
            binding.ivRealViewHigh.setText("腔内最高温度:"+df.format(status.temp_3584)+"°C");
            binding.ivRealViewLow.setText("腔内最低温度:"+df.format(status.temp_3587)+"°C");
            binding.ivRealViewAvg.setText("腔内平均温度:"+df.format(status.temp_avg_1)+"°C");
        } else if (event.getIndex() == 1) {
            binding.apSub.setImageBitmap(event.getImage());
            binding.ivRealViewHigh.setText("腔内最高温度:"+df.format(status.temp_3585)+"°C");
            binding.ivRealViewLow.setText("腔内最低温度:"+df.format(status.temp_3588)+"°C");
            binding.ivRealViewAvg.setText("腔内平均温度:"+df.format(status.temp_avg_2)+"°C");
        } else if (event.getIndex() == 2) {
            binding.apSub.setImageBitmap(event.getImage());
            binding.ivRealViewHigh.setText("腔内最高温度:"+df.format(status.temp_3586)+"°C");
            binding.ivRealViewLow.setText("腔内最低温度:"+df.format(status.temp_3589)+"°C");
            binding.ivRealViewAvg.setText("腔内平均温度:"+df.format(status.temp_avg_3)+"°C");
        }


    }

    int lastValue = 0;
    String lastTime = "";
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(SystemErrCodeEvent event){
        if(lastValue != event.getCurrentCode())
        {
            lastValue = event.getCurrentCode();
            lastTime = APPSPUtils.getInstance().getLastErrTime();
            if(lastTime.isEmpty()) {
                lastTime = DateTimeUtils.longToYMDHMS(System.currentTimeMillis());
                APPSPUtils.getInstance().putLastErrTime(lastTime);
            }

        }
        if(event.getCurrentCode() > 1)
        {
            binding.rlMainErr.setVisibility(View.VISIBLE);
            binding.tvMainErrContent.setText( event.getErrMsg());
            binding.tvMainErrTime.setText(lastTime);
            binding.tvMainErrTitle.setText(event.getErrTitle());
        }
        else
        {
            binding.rlMainErr.setVisibility(View.GONE);
            binding.tvMainErrContent.setText( "");
            binding.tvMainErrTime.setText("");
            binding.tvMainErrTitle.setText("");
            lastTime = "";
            APPSPUtils.getInstance().putLastErrTime("");
        }
    }

}